package com.pan.frame;

import com.pan.frame.other.TipsFrame;
import com.pan.server.MyServer;

import javax.swing.*;
import java.awt.*;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class ConnectFrame {



    private MyServer myServer;

    private JFrame frame;

    private JLabel titleLabel;

    private JLabel nameLabel;
    private JTextField nameText;

    private JLabel myInetAddressLabel;
    private JTextField myInetAddressText;

    private JTextField targetInetAddressText;
    private JLabel targetInetAddressLabel;

    private JTextField passwordText;
    private JLabel passwordLabel;

    private Font labelFont;
    private Font textFont;


    private JButton connectButton;
    private JButton createButton;



    public ConnectFrame(MyServer myServer) {
        this.myServer = myServer;

    }

    public void init(){

        frame = new JFrame("Pan.Qi.An");
        frame.setLayout(null);
        frame.setSize(400,300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);

        labelFont = new Font("SimSun", Font.PLAIN, 18);
        textFont = new Font("SimSun", Font.PLAIN, 18);

        titleLabel = new JLabel("连接界面");
        titleLabel.setFont(new Font("SimSun",Font.BOLD,24));
        titleLabel.setBounds(140,20,100,24);
        frame.add(titleLabel);

        nameLabel = new JLabel("设置昵称：");
        nameLabel.setFont(labelFont);
        nameLabel.setBounds(40,70,90,20);
        frame.add(nameLabel);

        nameText = new JTextField();
        nameText.setFont(labelFont);
        nameText.setBounds(140,70,180,22);
        frame.add(nameText);


        myInetAddressLabel = new JLabel("我的地址：");
        myInetAddressLabel.setFont(labelFont);
        myInetAddressLabel.setBounds(40,100,90,20);
        frame.add(myInetAddressLabel);

        InetAddress localHost = null;
        try {
            localHost = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        }
        myInetAddressText = new JTextField(localHost.getHostAddress()+":11451");
        myInetAddressText.setFont(textFont);
        myInetAddressText.setEditable(false);
        myInetAddressText.setBounds(140,100,180, 22);
        frame.add(myInetAddressText);


        targetInetAddressLabel = new JLabel("目的地址：");
        targetInetAddressLabel.setFont(labelFont);
        targetInetAddressLabel.setBounds(40,130,90,20);
        frame.add(targetInetAddressLabel);

        targetInetAddressText = new JTextField();
        targetInetAddressText.setFont(textFont);
        targetInetAddressText.setText("");//"192.168.31.43:11451"
        targetInetAddressText.setBounds(140,130,180, 22);
        frame.add(targetInetAddressText);

        passwordLabel = new JLabel("房间密码：");
        passwordLabel.setFont(labelFont);
        passwordLabel.setBounds(40,160,90,20);
        frame.add(passwordLabel);

        passwordText = new JTextField();
        passwordText.setFont(textFont);
        passwordText.setBounds(140,160,180, 22);
        frame.add(passwordText);

        connectButton = new JButton("连接对方");
        connectButton.setBounds(40,200,120,24);
        connectButton.setFont(textFont);
        connectButton.addActionListener((event)->{
            connect();
        });
        frame.add(connectButton);


        createButton = new JButton("创建房间");
        createButton.setBounds(200,200,120,24);
        createButton.setFont(textFont);
        createButton.addActionListener((event)->{
            create();
        });
        frame.add(createButton);


        frame.setVisible(true);
    }

    public void setEnable(boolean b){
        targetInetAddressText.setEditable(b);
        passwordText.setEditable(b);
        connectButton.setEnabled(b);
        createButton.setEnabled(b);
    }

    public boolean setName(){
        String name = nameText.getText();
        if (name==null||name.length()==0){
            return false;
        }else {
            myServer.setName(name);
            return true;
        }
    }

    public void connect(){
        String host = "";
        String pw = "";
        int port = 11451;
        try {
            pw = passwordText.getText();
            String text = targetInetAddressText.getText();
            String[] arr = text.split(":");
            host = arr[0];
            port = Integer.parseInt(arr[1]);
        }catch (Exception e){
            new Thread(()->{
                new TipsFrame("错误","目标地址错误");
            }).start();
            return;
        }
        boolean isName = setName();
        if (!isName){
            new Thread(()->{
                new TipsFrame("错误","请正确设置昵称");
            }).start();
            return;
        }
        myServer.connect(host, port,pw);
    }


    public void create(){

        boolean isName = setName();
        if (!isName){
            new Thread(()->{
                new TipsFrame("错误","请正确设置昵称");
            }).start();
            return;
        }
        String password = passwordText.getText();
        myServer.waitConnect(password);
    }

    public void close(){
        frame.dispose();
    }

    public static void main(String[] args) throws UnknownHostException {


    }

}
